function [dy,ds] = IB_AD(ts, t, y, in_s, in_gates, V_c, J_d)
%IB_AD defines the dynamics of the dendrite of a deep IB cell.
%   y = [V h_d m_d m_ARd m_KMd m_CaHd]
%   s = all synapses
%   V = all voltages

global IBIndices;
global LTSIndices;
global LTSCells;
global IB_ACells;

%J_d = 18;% (LK)  %-10?
g_NaFd = 125; g_KDRd = 10; g_ARd = 135; %(LK) %40; (to get alpha) 
g_Ld = 2;
g_sd = 0.2; 
g_Leak = 2; g_CaHd = 6.5; 
g_KMd = 0.75; 
tau_sLd_r = 0.5; tau_sLd_d = 20;
C = 1;

Vk_L = 80;
Vk_a = 0;

dy = zeros(6,1);
ds = zeros(size(in_s));

I_Leak = g_Leak*(70+y(1));
I_NaF = g_NaFd*(m_0e(y(1))^3)*y(2)*(y(1)-50);
I_KDR = g_KDRd*(y(3)^4)*(95+y(1));
I_AR = g_ARd*y(4)*(25+y(1));
I_KM = g_KMd*y(5)*(95+y(1));
I_CaH = g_CaHd*(y(6)^2)*(y(1)-125);
Is_sd = -1*g_sd*(V_c-y(1));
%Is_Ld = g_Ld*y(7)*(80+y(1));

Is_all(LTSIndices) = in_gates(LTSIndices).*in_s(LTSIndices)*(y(1)+Vk_L);
Is_all(IBIndices) = in_gates(IBIndices).*in_s(IBIndices)*(y(1)+Vk_a);

dy(1) = (-1/C)*(J_d+I_Leak+I_NaF+I_KDR+I_AR+I_KM+I_CaH+Is_sd+sum(Is_all));

dy(2) = (1/tau_he(y(1)))*(h_infe(y(1))-y(2));

dy(3) = (1/tau_me(y(1)))*(m_infe(y(1))-y(3));

dy(4) = (1/tau_mAR(y(1)))*(m_infAR(y(1),75)-y(4));

dy(5) = alpha_KM(y(1))*(1-y(5))-beta_KM(y(1))*y(5);

dy(6) = (1/.33)*(alpha_CaH(y(1))*(1-y(6))-beta_CaH(y(1))*y(6));

%dy(7) = synapse(y(7),tau_sLd_r,tau_sLd_d,V_s(1));

for i = 1:length(LTSIndices)
    j = LTSIndices(i);
    ds(j) = synapse(in_s(j), tau_sLd_r, tau_sLd_d, LTSCells(ts, 1, i));
end


end